﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rename.Util
{
    public partial class FileHelper
    {
        private const string DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        #region 获取文件大小并以B，KB，GB，TB
        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(string fileName,out string crTime,out string mTime)
        {
            string m_strSize = "";
            long FactSize = 0;
            FileInfo fileInfo = new FileInfo(fileName);
            FactSize = fileInfo.Length;
            crTime=fileInfo.CreationTime.ToString(DATE_FORMAT); 
            mTime=fileInfo.LastWriteTime.ToString(DATE_FORMAT);

            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " B";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " KB";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " MB";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " GB";
            return m_strSize;
        }
        #endregion

        #region 获取指定目录及子目录中所有文件列表
        /// <summary>
        /// 获取指定目录及子目录中所有文件列表
        /// </summary>
        /// <param name="directoryPath">指定目录的绝对路径</param>
        /// <param name="searchPattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。
        /// 范例："Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
        /// <param name="isSearchChild">是否搜索子目录</param>
        public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
        {
            //如果目录不存在，则抛出异常
            if (!Directory.Exists(directoryPath))
            {
                throw new FileNotFoundException();
            }
            try
            {
                if (isSearchChild)
                {
                    return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
                }
                else
                {
                    return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
        #endregion

        ///// <summary>
        ///// 获取格式化的文件大小（自动转换为KB、MB、GB等单位）
        ///// </summary>
        ///// <param name="filePath">文件路径</param>
        ///// <returns>格式化的文件大小字符串</returns>
        //public static string GetFormattedFileSize(string filePath)
        //{
        //    try
        //    {
        //        if (!File.Exists(filePath))
        //            return "文件不存在或无法访问";

        //        FileInfo fileInfo = new FileInfo(filePath);

        //        long bytes = fileInfo.Length;


        //        // 定义单位数组
        //        string[] units = { "B", "KB", "MB", "GB", "TB" };
        //        double size = bytes;
        //        int unitIndex = 0;

        //        // 转换单位直到合适的量级
        //        while (size >= 1024 && unitIndex < units.Length - 1)
        //        {
        //            size /= 1024;
        //            unitIndex++;
        //        }
        //        // 保留两位小数并格式化输出
        //        return $"{size:0.##} {units[unitIndex]}";
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine($"获取文件大小失败: {ex.Message}");
        //        return "0";
        //    }
        //}
    }
}
